package com.taiji.dicom4cloud.common.core.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taiji.dicom4cloud.common.core.model.CommonResponse;
import com.taiji.dicom4cloud.common.core.utils.AESUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/5/21
 **/
@AllArgsConstructor
@AutoConfigureAfter(DicomConfig.class)
@EnableConfigurationProperties(CommonProperties.class)
@Slf4j
public class DicomRestTemplate {

    private RestTemplate restTemplate;
    private CommonProperties commonProperties;


    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    public JSONObject login( String username,String password) {
        ResponseEntity<String> response;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        Map<String,Object>requestBody = new HashMap<>(16);
        requestBody.put("username", AESUtil.encode(username));
        requestBody.put("password", AESUtil.encode(password));
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        response = restTemplate.postForEntity(commonProperties.getUrl() + "/dicom-common-server/user/login", httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject( response.getBody());
        commonProperties.setToken(jsonObject.getString("access_token"));
        return jsonObject;
    }

    /**
     * 发送请求
     *
     * @param path
     * @param requestBody
     * @return
     */
    public CommonResponse postForEntity(String path, Map<String, Object> requestBody) {
        ResponseEntity<String> response;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","Bearer "+ commonProperties.getToken());
        headers.add("Content-Type", "application/json");
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        response = restTemplate.postForEntity(commonProperties.getUrl() + path, httpEntity, String.class);
        log.info("上传数据{}",response.getBody());
        CommonResponse res = JSONObject.toJavaObject(JSON.parseObject(response.getBody()), CommonResponse.class);
        return res;
    }
    public CommonResponse postForEntity(String path,String pdfPath) {
        ResponseEntity<String> response;
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","Bearer "+ commonProperties.getToken());
        //设置请求头
        headers.add("enctype","multipart/form-data");
        headers.add("Content-Type","multipart/form-data");

        //设置请求体，注意是LinkedMultiValueMap
        FileSystemResource fileSystemResource = new FileSystemResource(pdfPath);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", fileSystemResource);
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        response = restTemplate.postForEntity(commonProperties.getUrl() + path, files, String.class);
        log.info("上传文件{}",response.getBody());
        CommonResponse res = JSONObject.toJavaObject(JSON.parseObject(response.getBody()), CommonResponse.class);
        return res;
    }

}
